package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrdersettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

//提交预约信息
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
@Autowired
private OrdersettingDao ordersettingDao;
@Autowired
private MemberDao memberDao;
@Autowired
private OrderDao orderDao;

    public Result order(Map map) throws Exception {
        //查看用户预约的日期有没有设置预约人数
        //得到前端提交过来的时间
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = ordersettingDao.findNumberByOrderDate(DateUtils.parseString2Date(orderDate));
        if(orderSetting == null){
            return new Result(false , MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //查看预约人数是否已满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if(number - reservations <= 0 ){
            return new Result(false , MessageConstant.ORDER_FULL);
        }
        //查看用户是否重复预约,得到用户预约的日期，套餐和用户id
        String telephone = (String) map.get("telephone");//得到用户的手机号
        //根据用户手机号查询用户有没有注册成会员
        Member member = memberDao.findByTelephone(telephone);
        if(member != null){
            String setmealId = (String) map.get("setmealId"); //得到套餐id
            Integer memberId = member.getId();              //会员id
            Date order_Date = DateUtils.parseString2Date(orderDate); //得到预约日期
            Order order = new Order(memberId ,  order_Date , Integer.parseInt(setmealId));
            //得到了用户的id，预约的日期和套餐，去order表中查询有没有预约过
            List<Order> list = orderDao.findByCount(order);
            if(list != null && list.size() > 0){
                return new Result(false , MessageConstant.HAS_ORDERED);
            }
        }else {
            //查看用户有没有注册会员，没有注册则自动给他注册成会员
            //根据前台传递过来的数据，将用户添加到会员表中
            member = new Member();
            String name = (String) map.get("name");
            String sex = (String) map.get("sex");
            String idCard = (String) map.get("idCard");
            member.setName(name);
            member.setSex(sex);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            member.setIdCard(idCard);
            memberDao.add(member);
        }
        //预约成功,更新预约成功人数
        //体检预约成功后，先将预约信息保存到体检预约表中
        String setmealId = (String) map.get("setmealId");
        String orderType = (String) map.get("orderType");
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setSetmealId(Integer.parseInt(setmealId));
        order.setOrderType(orderType);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderDao.add(order);

        //将体检信息添加到体检表之后，更新预约成功人数，在orderetting表中
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        ordersettingDao.editReservatiosByOrderdate(orderSetting);

       return new Result(true , MessageConstant.ORDER_SUCCESS , order.getId());
    }

    //根据预约id查询预约信息
    public Map findById(Integer id) {
        Map map = orderDao.findById(id);
        if(map != null){
            Date orderDate = (Date) map.get("orderDate");
            try {
                map.put("orderDate" , DateUtils.parseDate2String(orderDate));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

}
